/*
 * @Description:
 * @FilePath: /glm_h5wxgzh/utils/tools.js
 */
// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null;
  }
  const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    } else if (typeof time === "string") {
      time = time
        .replace(new RegExp(/-/gm), "/")
        .replace("T", " ")
        .replace(new RegExp(/\.[\d]{3}/gm), "");
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}
// 判断是否是手机号
export function isPhoneNumber(phone) {
  var pattern = /^1[3-9]\d{9}$/;
  return pattern.test(phone);
}
// 表格时间格式化
export function formatDate(cellValue) {
	if (cellValue == null || cellValue == '') return '' 
	var date = new Date(cellValue)
	var year = date.getFullYear()
	var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
	var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
	var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
	var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
	return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}
// 阿拉伯数字转汉字
export function numToChinese(num){
  num = Math.floor(num);
	var chinese = "";
	var digits = Math.floor(Math.log10(num)) + 1;
	var digit = ['零','一','二','三','四','五','六','七','八','九'];
	var times = ['','十','百','千','万'];
	if(digits >= 9){
		var quotient = Math.floor(num / Math.pow(10,8));
		var remainder = num % Math.pow(10,8);
		var remainderDigits = Math.floor(Math.log10(remainder)) + 1;
		return toChinese(quotient) + '亿' + (remainderDigits < 8 ? "零" : "") + (remainder > 0 ? toChinese(remainder) : "");
	}
	if(digits == 1) return digit[num];
	if(digits == 2){
		var quotient = Math.floor(num / 10);
		var remainder = num % 10;
		if(quotient > 1){
			chinese = digit[quotient];
		}
		chinese = chinese + times[1];
		if(remainder > 0){
			chinese = chinese + digit[remainder];
		}
		return chinese;
	}
	if(digits > 5){
		var quotient = num / Math.pow(10,4);
		var remainder = num % Math.pow(10,4);
		var remainderDigits = Math.floor(Math.log10(remainder)) + 1;
		return toChinese(quotient) + '万' + (remainderDigits < 4 ? "零" : "") + (remainder > 0 ? toChinese(remainder) : "");
	}
	for(var index = digits;index >= 1;index--){
		var number = Math.floor(num / Math.pow(10,index - 1) % 10);
		if(number > 0){
			chinese = chinese + digit[number] + times[index - 1];
		}else {
			if(index > 1) {
				var nextNumber = Math.floor(num / Math.pow(10,index - 2) % 10);
				if(nextNumber > 0 && index > 1){
					chinese = chinese + digit[number];
				}
			}
		}
	}
	return chinese;
}

// 千分位分割符
export function format_with_array(number = 0) {
  // 判断是否为负数
  let isNegative = Number(number) < 0;
  if (isNegative) {
    number = (number + "").substring(1);
  }
  // 转为字符串，并按照.拆分
  const arr = (number + "").split(".");
  // 整数部分再拆分
  const int = arr[0].split("");
  // 小数部分
  const fraction = arr[1] || "";
  // 返回的变量
  let r = "";
  int.reverse().forEach(function (v, i) {
    // 非第一位并且是位值是3的倍数，添加“,”
    if (i !== 0 && i % 3 === 0) {
      r = v + "," + r;
    } else {
      // 正常添加字符(这是好写法)
      r = v + r;
    }
  });
  // 整数部分和小数部分拼接
  if (isNegative) {
    return "-" + r + (!!fraction ? "." + fraction : "");
  } else {
    return r + (!!fraction ? "." + fraction : "");
  }
}

/* 字符串转数字保留几位小数 */
export function strToFixed(str = 0, n = 0) {
  const num = parseFloat(str);
  if (isNaN(num)) {
    return str;
  }
  const roundedNum = num.toFixed(n);
  return roundedNum.toString();
}
